package com.changanw.permiss.comfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author changanw
 * @version ncc1.0
 * @description TODO
 * @date 2023/6/27 13:25
 */
@Configuration
public class SecurityConfig {
  @Bean
  UserDetailsService us(){
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(User.withUsername("changanw").password("{noop}123")
            //给用户设置角色
            .roles("admin")
            //给用户设置两个权限，
            //.authorities("system:user:add","system:user:delete")
            .authorities("system:user:*")
            .build());
    return manager;
  }
  @Bean
  CustomMethodSecurityExpressionHandler customMethodSecurityExpressionHandler(){
    return new CustomMethodSecurityExpressionHandler();
  }
  @Bean
  SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.csrf().disable()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .permitAll();
    return httpSecurity.build();
  }
}
